package com.gxa.financial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.R;
import com.gxa.common.entity.financial_management.PayRecord;
import com.gxa.financial.mapper.PayRecordMapper;
import com.gxa.financial.service.PayRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class PayRecordServiceImpl extends ServiceImpl<PayRecordMapper, PayRecord> implements PayRecordService {

    @Autowired
    private PayRecordMapper payRecordMapper;


    //分页
    @Cacheable(value = "payRecordList",key = "'selectPayRecordList'")
    @Override
    public R selectPayRecordList(Integer currentPage, Integer pageSize) {
        Page page = new Page(currentPage,pageSize);
        Page selectPage = payRecordMapper.selectPage(page, null);
        R r = new R();
        if (selectPage.getRecords() != null) {
            Map payRecordList = new HashMap();
            payRecordList.put("totalSize",selectPage.getTotal());
            payRecordList.put("totalPage",selectPage.getPages());
            payRecordList.put("totalData",selectPage.getRecords());
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(payRecordList);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }

    //条件查询
    @Override
    public R selectPayRecord(Integer currentPage, Integer pageSize, PayRecord payRecord) {
        Page page = new Page(currentPage,pageSize);
        QueryWrapper qw = new QueryWrapper();
        qw.eq(payRecord.getTransactionNo() != null,"transaction_no",payRecord.getTransactionNo())
                .eq(payRecord.getOrderType() != null,"order_type",payRecord.getOrderType())
                .eq(payRecord.getAccountType() != null,"account_type",payRecord.getAccountType())
                .eq(payRecord.getNickname() != null,"nickname",payRecord.getNickname())
                .eq(payRecord.getPhone() != null,"phone",payRecord.getPhone())
                .eq(payRecord.getStatus() != null,"status",payRecord.getStatus())
                .between(payRecord.getStartTime() != null&&payRecord.getEndTime() != null,"create_time",payRecord.getStartTime(),payRecord.getEndTime())
                .lt(payRecord.getStartTime() == null && payRecord.getEndTime() != null,"create_time",payRecord.getEndTime());
        Page selectPage = payRecordMapper.selectPage(page, qw);
        R r = new R();
        if (selectPage.getRecords() != null) {
            Map payRecordList = new HashMap();
            payRecordList.put("totalSize",selectPage.getTotal());
            payRecordList.put("totalPage",selectPage.getPages());
            payRecordList.put("totalData",selectPage.getRecords());
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(payRecordList);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }

    //
    @Override
    public R selectPayRecordByNo(Long transactionNo) {
        PayRecord payRecord = payRecordMapper.selectById(transactionNo);
        R r = new R();
        if (payRecord != null) {
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(payRecord);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }

    @Override
    public R updatePayRecordStatus(PayRecord payRecord) {
        payRecord.setStatus((byte)2);
        payRecord.setCompletionTime(new Date());
        int i = payRecordMapper.updateById(payRecord);
        R r = new R();
        if (i > 0) {
            r.setCode(200);
            r.setMessage("支付成功");
        }else {
            r.setCode(500);
            r.setMessage("支付失败");
        }
        return r;
    }

    @Override
    public R updatePayRecordFailStatus(PayRecord payRecord) {
        payRecord.setStatus((byte)0);
        payRecord.setCompletionTime(new Date());
        int i = payRecordMapper.updateById(payRecord);
        R r = new R();
        if (i > 0) {
            r.setCode(200);
            r.setMessage("交易关闭成功");
        }else {
            r.setCode(500);
            r.setMessage("交易关闭失败");
        }
        return r;
    }


    @Override
    public R addPayRecord(PayRecord payRecord) {
        //查询汇率返回实付款



        payRecord.setCreateTime(new Date());
        int insert = payRecordMapper.insert(payRecord);
        R r = new R();
        if (insert > 0) {
            r.setCode(200);
            r.setMessage("添加成功");
        }else {
            r.setCode(500);
            r.setMessage("添加失败");
        }
        return r;
    }
}
